package com.hjy.demosms;

import java.util.HashMap;
import java.util.Set;

import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Test;

import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;

import com.hjy.utils.SmsUtil;

public class SmsTest {
	
	@Test
	public void test(){
		String code = RandomStringUtils.randomNumeric(4);
		System.out.println(code);
	}
	
	@Test
	public void test2(){
		
		SmsUtil.sendSms("18975577597", "1", new String[]{RandomStringUtils.randomNumeric(4),"5"});
	}

	@Test
	public void testSms() {
		
		String serverIp = "app.cloopen.com";
	    //请求端口
	    String serverPort = "8883";
	    //主账号,登陆云通讯网站后,可在控制台首页看到开发者主账号ACCOUNT SID和主账号令牌AUTH TOKEN
	    String accountSId = "8a216da86715511501671f4e20e10767";
	    String accountToken = "b88b14ba73264692a3dce2bcf3ecc5ec";
	    //请使用管理控制台中已创建应用的APPID
	    String appId = "8a216da86715511501671f4e2127076d";
	    
	    
	    CCPRestSmsSDK sdk = new CCPRestSmsSDK();
	    sdk.init(serverIp, serverPort);
	    sdk.setAccount(accountSId, accountToken);
	    sdk.setAppId(appId);
	    sdk.setBodyType(BodyType.Type_JSON);
	    
	    //收短信的号码
	    String to = "18975577597";
	    
	    // 模板1的形式为：【云通讯】您使用的是云通讯短信模板，您的验证码是{1}，请于{2}分钟内正确输入。
	    String templateId= "1";
	    
	    //随机生成4位数字作为验证码
	    String code = RandomStringUtils.randomNumeric(4);
	    //过期时间,单位分钟
	    String expire = "5";
	    
	    String[] datas = {code,expire};
	    
	    //请求发送短信
	    HashMap<String, Object> result = sdk.sendTemplateSMS(to,templateId,datas);
	    
	    if("000000".equals(result.get("statusCode"))){
	        //正常返回输出data包体信息（map）
	        HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
	        Set<String> keySet = data.keySet();
	        for(String key:keySet){
	            Object object = data.get(key);
	            System.out.println(key +" = "+object);
	        }
	    }else{
	        //异常返回输出错误码和错误信息
	        System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
	    }
	}
}
